<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/31
 * Time: 9:04
 * mid :14
 */
class jxbus extends admin
{
    public function __construct()
    {
        parent::__construct();
        array_walk_recursive($_GET, 'safe_filter');
        array_walk_recursive($_POST, 'safe_filter');
    }
    //添加驾校:
    public function car_insert(){
        $date['car_model'] = trim($_POST['car_model']);
        $date['car_num']   = trim($_POST['car_num']);
        $date['addtime']   = time();
        $date['school_id']     = $_SESSION['client']['jx_id'];
        $date['jl_id']     = $_POST['id'];
        $date['status']    = $_POST['status'];
        $rs = db()->insert('jx_bus',$date);
        if($rs) {
            insertlogs('create','添加车辆信息(ID:'.$rs.')',$date,$rs);
            $return = array('status'=>1,'info'=>'添加成功');
        }else{
            $return = array('status'=>0,'info'=>'添加失败');
        }
        echo json_encode($return);
    }
    public function car_adds(){
        $rs= db()->getAll("select id,real_name from x_user WHERE user_type=2 and  school_id=".$_SESSION['client']['jx_id']);
        if(!$rs) $rs=array();
        echo json_encode(array('status'=>1,'info'=>$rs));
    }
    public function modify(){
        $date['car_model'] = trim($_POST['car_model']);     //车辆品牌
        $date['car_num']  = trim($_POST['car_num']);        //车牌号码
        $date['status']   = $_POST['status'];               //正常 维修
        $date['jl_id']    = intval($_POST['real_name']);
        $id = intval($_POST['id']);                         //所编辑id
        $rs = db()->update('jx_bus',$date,'id='.$id);
        insertlogs('update','修改车辆信息(ID:'.$id.')',$date,$id);
        if($rs !== false) $return = array('status'=>1,'info'=>$_POST);
        else $return = array('status'=>0,'info'=>'失败');
        echo json_encode($return);
    }
    public function dels(){
        $id = (int)$_POST['id'];
        $rs = db()->delete('jx_bus','id=' . $id);
        insertlogs('delete','删除车辆信息(ID:'.$id.')',$id,$id);
        if(!$rs) $rs = array();
        echo json_encode(array('status'=>1,'info'=>$rs));
    }
    //批量删除
    public function pl_del(){
        foreach($_POST as $id){
            $del_sql = 'delete from jx_bus where id = ' . $id['id'] . ' ';
            $rs = db()->query($del_sql);
            if($rs)  insertlogs('delete','批量删除车辆信息, (ID:'.$id['id'].')',$id['id'],$id['id']);
        }
        echo json_encode(array('status'=>1));
    }
    //编辑单条数据;
    public function popup_action(){
        $popup_id = intval($_POST['popup_id']);
        $popup_row = db()->getRow("select id,car_model,car_num,status,jl_id,school_id from jx_bus WHERE id=".$popup_id);
        $popup_row['real_name'] = db()->getOne("select real_name from x_user WHERE id=".$popup_row['jl_id']);
        if($popup_row['jl_id']==0 || $popup_row['jl_id'] == -1){
            $popup_row['real_name'] ='未指定';
        }
        $popup_row['jl_row'] = db()->getAll("select id,real_name from x_user WHERE user_type=2 AND school_id=".$_SESSION['client']['jx_id']);
        if($popup_row) $return = array('status'=>1,'info'=>$popup_row);
        else $return = array('status'=>0,'info'=>'系统繁忙,请稍后重试','jl_id'=>$popup_row['jl_id']);
        echo json_encode($return);
    }
    public function select(){
        $where =  ' where 1=1 and school_id = "' . $_SESSION['client']['jx_id'] . '"';
        if($_POST['search']) {
         $search = trim($_POST['search']);
         $where.= " and car_num like '%".$search."%' ";   
        }
        $total_sql = 'select count(*) from  jx_bus'.$where;
        $total=db()->getOne($total_sql);
        $pagesize = 6;
        if(isset($_POST['p']) && (int) $_POST['p'] > 0) $p = (int) $_POST['p'];
        else $p =1;
        $limit = ' limit '.($p-1)*$pagesize.','.$pagesize;
       $rs = db()->getAll("select id,car_model,car_num,is_kt,addtime,jl_id,status from jx_bus ".$where." order by id desc".$limit);
       if(!$rs) $rs = array();
        $page = array('total'=>$total,'pagesize'=>$pagesize,'curpage'=>$p);
           for ($i=0; $i < count($rs) ; $i++) { 
               $rs[$i]['addtime'] = date('Y-m-d',$rs[$i]['addtime']);
               $rs[$i]['real_name'] = db()->getOne("select real_name from x_user where id=".$rs[$i]['jl_id']);
               if($rs[$i]['real_name']==false) $rs[$i]['real_name'] = '----';
           }
       $return = array('status'=>1,'info'=>$rs,'search'=>$_POST['search'],'page'=>$page);
       echo json_encode($return);
    }

    public function index(){
        $pagesize=C('PAGE_SIZE');
		$url = '/admin/jxbus/';
		$where = ' where 1=1 ';
		if($_GET['keyword']!=''){
			$keyword = trim($_GET['keyword']);
			$where .= " and car_num like '%$keyword%' ";
			$url .= '?keyword='.$keyword;
		}
        $count = db()->getOne("select count(*) from jx_bus".$where);
        $page = $_GET['page']>0?$_GET['page']:1;
        $pages = ceil($count/$pagesize);
        if($page>=$pages) $page = $pages;
        $start = ($page-1)*$pagesize;
        $list = db()->getAll("select id,car_model,car_num,is_kt,addtime from jx_bus ".$where." order by id desc limit $start,$pagesize");
        $pages_s=pages($pagesize,$count,$url);
        $tpl = $this->Template();
        $tpl->assign('pages',$pages_s);
        $tpl->assign('list', $list);
        $tpl->display();
    }

    public function add()
    {
        if(IS_AJAX&&IS_POST){
            if(!isset($_POST['car_model'])||empty($_POST['car_model'])) $this->ajaxReturn(array('status'=>'n','info'=>'请填写车辆名称'));
            if(!isset($_POST['car_num'])||empty($_POST['car_num'])) $this->ajaxReturn(array('status'=>'n','info'=>'请填写汽车车牌号'));
            if(!isset($_POST['jx_id'])||empty($_POST['jx_id'])) $this->ajaxReturn(array('status'=>'n','info'=>'请选择所属驾校'));
            $data['is_kt']= isset($_POST['is_kt']) ? $_POST['is_kt'] : 0;  //默认为0,0表示没空调
            $data['car_model']= trim($_POST['car_model']);  //车辆型号
            $data['car_num']= trim($_POST['car_num']);  //车牌号
            $data['jx_id'] = (int)$_POST['jx_id'];   //所属驾校名字
            $data['jl_id'] = (int)$_POST['jl_id'];     //所属教练;
            $data['picture'] = trim($_POST['picture']);  //车辆图片地址;
            $data['addtime']=NOW; //添加时间;
            $insert_id=db()->insert('jx_bus',$data);
            
            if($insert_id) {
			$userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
			$infoid = $insert_id;
			$method = 'create';
			$mid = 13;
			$logs = json_encode($data);
			$remark = '添加车辆信息(ID:'.$insert_id.')';
			insertlogs($userData,$logs,$infoid,$method,$mid,$remark);
                $this->ajaxReturn(array('status'=>'y','info'=>'添加成功'));
            }else{
                $this->ajaxReturn(array('status'=>'n','info'=>'添加失败'));
            }
        }else{
            $tpl=$this->Template();
            $jx = db()->getAll("select name,id from x_jiaxiaoinfo where ecityid='1703'");  //驾校
            $tpl->assign('jx',$jx);
            $tpl->display();
        }
    }

    public function edit()
    {
        if(IS_AJAX&&IS_POST){
            $id=(int)$_GET['id'];
            if(!isset($_POST['car_model'])||empty($_POST['car_model'])) $this->ajaxReturn(array('status'=>'n','info'=>'更新此信息有误'));
            if(!isset($_POST['car_num'])||empty($_POST['car_num'])) $this->ajaxReturn(array('status'=>'n','info'=>'更该此信息有误,请重新填写'));
            $data['car_model']=trim($_POST['car_model']);
            $data['car_num'] = trim($_POST['car_num']);
            $data['jx_id'] = intval($_POST['jx_id']);
            $data['jl_id'] = intval($_POST['jl_id']);
			$data['picture'] = trim($_POST['picture']);
            $data['is_kt'] = $_POST['is_kt'];
            $data['addtime']= NOW;   //更新时间,以当前日期为准;
            $insert_id=db()->update('jx_bus',$data,'id='.$id);

			$userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
			$logs = json_encode($data);
			$infoid = $id;
			$method = 'update';
			$mid = 13;
			$remark = '修改车辆信息(ID:'.$id.')';
			insertlogs($userData,$logs,$infoid,$method,$mid,$remark);

            if($insert_id){
                $this->ajaxReturn(array('status'=>'y','info'=>'更新成功'));
            }
            else $this->ajaxReturn(array('status'=>'n','info'=>'更新失败id为'.$id));
        }else{
            $id=(int)$_GET['id'];
            $res = db()->getRow("select * from jx_bus WHERE id=" . $id);
			$school_id = $res['jx_id'];
            $jx = db()->getAll("select id,name from x_jiaxiaoinfo where ecityid='1703'");
			$jls = db()->getAll("select id,real_name from x_user where school_id='$school_id' and user_type=2");
            $tpl = $this->Template();
            $tpl->assign('res', $res);
            $tpl->assign('jx', $jx);
			$tpl->assign('jls', $jls);
            $tpl->display();
        }
    }
    public function del()
    {
        if (IS_AJAX) {
            $id = (int)$_GET['id'];
            $delete = db()->delete('jx_bus', 'id=' . $id);
            if ($delete){
                $userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
                $infoid = $id;
                $method = 'delete';
                $mid = 13;      //所属模块
                $remark = '删除车辆信息(车辆ID:'.$id.')';
                insertlogs($userData,$logs='',$infoid,$method,$mid,$remark);
            $this->ajaxReturn(array('status' => 'y', 'info' => '删除成功'));
            }else{
                $this->ajaxReturn(array('status' => 'n', 'info' => '删除失败'));
            }
        }
    }

    public function uploads()
    {
        if($_FILES['error']>0){
            echo '上传的文件有误';
        }
        //把图片移动指定目录
        $path = 'upload/jxbus/'.date("Ym").'/';
		mkDirs($path);
		$file = $_FILES['file']['name'];
		$extension=substr($file, strrpos($file, '.')+1);
        $path.=mt_rand(1000,9999).md5($_FILES['file']['tmp_name']).'.'.$extension;
        $path = iconv('UTF-8','GB2312',$path);

        //参数1:自定义地址,参数2:移动的新地址;
        if(move_uploaded_file($_FILES['file']['tmp_name'],$path)){
             echo  json_encode(array('status'=>'ok','info'=>'/'.$path));
            }
            else{
                echo  json_encode(array('status'=>'n','info'=> '上传失败'));
            }
    }
	public function getjiaolian(){
		$jxid=intval($_GET['jxid']);
		$jlid=intval($_GET['jlid']);
		$jls = getjiaolianByJx($jxid);
		$str = '<option value="">请选择教练</option>';
		if(count($jls)>0){
			foreach($jls AS $v){
				if($v['id']==$jlid){
					$c = ' selected';
				}else{
					$c = '';
				}
				$str .= '<option value="'.$v['id'].'"'.$c.'>'.$v['real_name'].'</option>';
			}
		}else{
			
		}
		echo $str;
	}

}






















